/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * CodeChef practice: Transform the Expression.
 * @author quanghx
 */
public class Main {

    public final static String operators = "+-*:^";
    public final static Character LEFT_PARENTHESIS = '(';
    public final static Character RIGHT_PARENTHESIS = ')';

    /**
     * Main program to submiss to codechef.
     * @param args
     */
    public static void main(String[] args) {

        try {
            // Prepare parameter.
            int count;
            String line;

            java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

            // Read number of test-case
            line = r.readLine();
            if (line == null) {
                throw new IllegalArgumentException("number of test not valid");
            }
            count = Integer.valueOf(line);

            // Printout number of result.
            System.out.println(count);

            // Execute test-case.
            for (int i = 0; i < count; i++) {
                line = r.readLine();

                if (line == null) {
                    throw new IllegalArgumentException("number of test not valid");
                }

                line = line.trim();

                System.out.println(cal(line));
            }
        } catch (IOException e) {
        }
    }

    /**
     * Caculate infix to postfix expression.
     * @param infix input expression.
     * @return postfix expression.
     */
    public static String cal(String infix) {
        // Store output expression.
        StringBuilder postfix = new StringBuilder(infix.length());

        // Store operators
        Stack<Character> operatorStack = new Stack<Character>();

        // Store output queue
        Queue<Character> outputQueue = new LinkedList<Character>();

        // Read a token
        Character token;
        for (int i = 0; i < infix.length(); i++) {
            token = infix.charAt(i);

            // If the token is an operator.
            if (operators.contains(String.valueOf(token))) {

                // While there is a greater precedence operator o2, at the top of the stack and it
                while (!operatorStack.isEmpty() &&
                        operators.contains(String.valueOf(operatorStack.peek())) &&
                        compareOperator(token, operatorStack.peek()) >= 0) {

                    popStackToQueue(operatorStack, outputQueue);
                }

                operatorStack.push(token);

            } else if (token.equals(LEFT_PARENTHESIS)) {
                operatorStack.push(token);

            } else if (token.equals(RIGHT_PARENTHESIS)) {

                while (!operatorStack.isEmpty() &&
                        !operatorStack.peek().equals(LEFT_PARENTHESIS)) {

                    popStackToQueue(operatorStack, outputQueue);
                }

                if (operatorStack.isEmpty()) {
                    throw new IllegalArgumentException(infix + " not a valid infix expression");
                } else {
                    operatorStack.pop();
                }

            } else {
                outputQueue.offer(token);
            }

        }

        // Put all remain operator to output queue.
        while (!operatorStack.isEmpty()) {
            popStackToQueue(operatorStack, outputQueue);
        }

        // Create postfix string.
        while (!outputQueue.isEmpty()) {
            postfix.append(outputQueue.poll());
        }

        return postfix.toString();
    }

    /**
     * Pop character from stack to queue.
     * @param stack
     * @param queue
     */
    public static void popStackToQueue(Stack<Character> stack, Queue<Character> queue) {
        if (!stack.empty()) {
            queue.offer(stack.pop());
        }
    }

    /**
     * Compare two operator precedence level.
     * @param a first operator to compare.
     * @param b second operator to compare.
     * @return b.precedence - a.precedence.
     */
    public static int compareOperator(Character a, Character b) {
        if (operators.contains(String.valueOf(a)) &&
                operators.contains(String.valueOf(b))) {

            return operators.indexOf(b) - operators.indexOf(a);

        } else {
            throw new IllegalArgumentException(
                    "a= " + a + " or b= " + b + " not valid operators");
        }
    }
}
